package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.query.OrderChartsQueryObject;
import cn.wolfcode.wms.query.SaleChartsQueryObject;
import cn.wolfcode.wms.service.*;
import cn.wolfcode.wms.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chart")
public class ChartsController {
    @Autowired
    private IOrderChartsService orderChartsService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private ISaleChartsService saleChartsService;
    @RequestMapping("order")
    @RequiredPermission("订货报表")
    public String order(@ModelAttribute("qo") OrderChartsQueryObject qo, Map map){
        map.put("groupTypes",OrderChartsQueryObject.groupTypes);
        map.put("charts",orderChartsService.listOrderCharts(qo));
        map.put("suppliers",supplierService.list());
        map.put("brands",brandService.list());
        return "charts/order";
    }
    @RequestMapping("sale")
    @RequiredPermission("销售报表")
    public String sale(@ModelAttribute("qo") SaleChartsQueryObject qo, Map map){
        map.put("groupTypes",SaleChartsQueryObject.groupTypes);
        map.put("charts",saleChartsService.listSaleCharts(qo));
        map.put("clients",clientService.list());
        map.put("brands",brandService.list());
        return "charts/sale";
    }
    @RequestMapping("saleChartByBar")
    @RequiredPermission("销售报表-柱状图")
    public String saleChartByBar(@ModelAttribute("qo") SaleChartsQueryObject qo, Map map){
        List<BigDecimal> totalAmounts = new ArrayList<>();
        List<String> groupTypes = new ArrayList<>();
        List<Map<String, Object>> saleCharts = saleChartsService.listSaleCharts(qo);
        for (Map<String, Object> saleChart : saleCharts) {
            totalAmounts.add((BigDecimal) saleChart.get("totalAmount"));
            groupTypes.add((String) saleChart.get("groupType"));
        }
        map.put("groupTypes", JSON.toJSONString(groupTypes));
            map.put("totalAmounts",totalAmounts);
            map.put("groupBy",JSON.toJSONString(SaleChartsQueryObject.groupTypes.get(qo.getGroupBy())));
            return "charts/saleChartByBar";
        }
        @RequestMapping("saleChartByPie")
        @RequiredPermission("销售报表-柱状图")
        public String saleChartByPie(@ModelAttribute("qo") SaleChartsQueryObject qo, Map map){
            List<String> groupTypes = new ArrayList<>();
            List<Map<String, Object>> saleCharts = saleChartsService.listSaleCharts(qo);
            List<Map<String,Object>> datas = new ArrayList<>();
            BigDecimal maxNumber = BigDecimal.ZERO;
            for (Map<String, Object> saleChart : saleCharts) {
                if(maxNumber.compareTo((BigDecimal) saleChart.get("totalAmount")) < 0){
                    maxNumber = (BigDecimal) saleChart.get("totalAmount");
                }
                groupTypes.add((String) saleChart.get("groupType"));
                HashMap<String, Object> data = new HashMap<>();
                data.put("value",saleChart.get("totalAmount"));
                data.put("name",saleChart.get("groupType"));
            datas.add(data);
        }
        map.put("groupTypes", JSON.toJSONString(groupTypes));
        map.put("datas", JSON.toJSONString(datas));
        map.put("maxNumber",maxNumber);
        map.put("groupBy",JSON.toJSONString(SaleChartsQueryObject.groupTypes.get(qo.getGroupBy())));
        return "charts/saleChartByPie";
    }
}
